package handle

import (
	"mgs/global"
	"mgs/handle/api"
	"mgs/toolkit/ip2region"
	"mgs/toolkit/phone"
	"net/http"
)

// PhoneSearch 手机号归属地查询
func PhoneSearch(rw http.ResponseWriter, r *http.Request) {
	search := r.FormValue("search")
	if len(search) == 0 {
		api.Failed(rw, "查询参数search不能为空")
		return
	}
	info, err := phone.Find(search)
	if err != nil {
		api.Failed(rw, err.Error())
		return
	}
	api.Success(rw, info)
	return
}

var ipDat *ip2region.Ip2Region

// IpSearch IP归属地查询
func IpSearch(rw http.ResponseWriter, r *http.Request) {
	search := r.FormValue("search")
	if len(search) == 0 {
		api.Failed(rw, "查询参数search不能为空")
		return
	}
	if ipDat == nil {
		// 初始化加载文件
		ipDat, _ = ip2region.New(global.CONFIG.System.IpDat)
	}
	info, err := ipDat.MemorySearch(search)
	if err != nil {
		api.Failed(rw, err.Error())
		return
	}
	api.Success(rw, info)
	return
}
